<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\Config;
use app\api\model\Token;
use app\common\model\AliPay;
use app\common\model\RebateLog;
use EasyWeChat\Factory;
use think\Exception;
use think\facade\Db;
use think\Model;


class ItemModel extends Model
{
    protected $autoWriteTimestamp = true;

    protected $table = 'yzn_item';

    public function douyin()
    {
        return $this->belongsTo(DouyinModel::class, 'dy_id', 'id');
    }

    public function cate()
    {
        return $this->belongsTo(CateModel::class, 'cid', 'id');
    }

    public function staff()
    {
        return $this->belongsTo(StaffModel::class, 'uid', 'id');
    }


    public function getDyLastItemTime($params){
        if (!$params['dy_id']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        //获取该用户上次发布视频的时间
        $where[] = ['status', '=', 1];
        $where[] = ['dy_id', '=', $params['dy_id']];
        $lastItem = self::where($where)->order('id desc')->field('dy_id,create_time')->find();
        $lastItem['limitTime'] = (new Config())->where(['name' => 'lastPush'])->value('value') * 60;
        $lastItem['lastPushTime'] = strtotime($lastItem['create_time']);
        unset($lastItem['create_time']);
        return ['code' => 200, 'msg' => '获取成功','data' => ['item' => $lastItem]];
    }


    public function getDyItemList($params = array())
    {
        if (!$params['dy_id']) {
            return ['code' => 201, 'msg' => '缺少参数'];
        }
        if($params['cid']){
            $map[] = ['yzn_item.cid', '=', $params['cid']];
        }
        $map[] = ['dy_id', '=', $params['dy_id']];
        $map[] = ['yzn_item.status', '<>', 0];
        $list = self::where($map)
            ->withJoin(['cate'])
            ->order('id desc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);

        foreach ($list as $row) {
            $row->getRelation('cate')->visible(['cate_name']);
            $row->visible(['id','dy_id','comment_count','title','digg_count','download_count','play_count','share_count','status','kind','hashtag','cover']);
        }

        return ['code' => 200, 'msg' => '获取成功','data' => ['list' => $list]];
    }
}
